package search

import (
	"fmt"
	"github.com/go-ego/riot"
	"github.com/go-ego/riot/types"
	"maki/config"
)

var Engine = riot.Engine{}

const (
	dictFile      = "data/dict/dictionary.txt"
	stopTokenFile = "data/dict/stop_tokens.txt"
)

func init() {
	Engine.Init(types.EngineOpts{
		Using:         1,
		GseDict:       dictFile,
		StopTokenFile: stopTokenFile,
		IndexerOpts: &types.IndexerOpts{
			IndexType: types.FrequenciesIndex,
		},
		IDOnly: true,
		//如果你希望使用持久存储，启用下面的选项
		//默认使用leveldb持久化，如果你希望修改数据库类型
		//请用 StoreEngine:"" 或者修改 Riot_Store_Engine 环境变量
		UseStore:    true,
		StoreFolder: config.ServerConfig.StoreFolder,
		StoreEngine: "bg",
	})
	Engine.Flush()
	fmt.Println("Data indexed count: ", Engine.NumDocsIndexed())
}

func SearchDoc(keyword string) []uint64 {
	resps := Engine.SearchID(types.SearchReq{
		Text: keyword,
		RankOpts: &types.RankOpts{
			MaxOutputs: 1000,
		},
	})
	var results []uint64
	for _, doc := range resps.Docs {
		results = append(results, doc.DocId)
	}
	return results
}

func RemoveDoc(id uint64) {
	Engine.RemoveDoc(id)
}
